package MyZipStream;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class demo2 {
    public static void main(String[] args) throws IOException{
        File src = new File("D:\\File\\a.txt");
        File dest = new File("D:\\File\\");
        toZip(src,dest);
    }
    public static void toZip(File src,File dest)throws IOException {
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(new File(dest,"a.zip")));
        ZipEntry entry = new ZipEntry("a.txt");
        zos.putNextEntry(entry);
        FileInputStream fis = new FileInputStream(src);

        int b;
        while ((b = fis.read()) != -1){
            zos.write(b);
        }
        fis.close();
        zos.closeEntry();
        zos.close();
    }
}
